﻿using Leap;
using System;
using System.Collections.Generic;

namespace GameLove
{
    public class SingleLeaplisteners : Listener
    {
        public override void OnInit(Controller arg0)
        {
            base.OnInit(arg0);
        }

        public override void OnConnect(Controller arg0)
        {
            base.OnConnect(arg0);
        }

        public override void OnExit(Controller arg0)
        {
            base.OnExit(arg0);
        }

        public override void OnFocusLost(Controller arg0)
        {
            base.OnFocusLost(arg0);
        }

        /* waktu untuk menghandle listener, 
         * hal ini dikarenakan listener akan selalu mengupdate event */
        private long currTime;
        private long prevTime;
        private long timeTrans;

        public List<FingerPoint> fingerPoint = new List<FingerPoint>();

        public override void OnFrame(Controller arg0)
        {
            //base.OnFrame(arg0);

            /*Dapatkan nilai frame saat listener berjalan
             * */
            Frame curFrame = arg0.Frame();

            /* inisialisasi waktu sekarang dengan mengisi waktu yang ada pada frame
             * isikan timeTrans dengan perpindahan waktu antara waktu sekarang dengan sebelumnya
             */
            currTime = curFrame.Timestamp;
            timeTrans = currTime - prevTime;

            /* Inisialisasi Banyaknya jari yang aktif
             * */
            short numActiveFinger = 0;

            if (timeTrans > 100)
            {
                if (curFrame.Hands.IsEmpty.Equals(false))
                { 
                    /* Dapatkan semua jari dari jari yang terdeteksi oleh frame
                     * */
                    FingerList handFinger = curFrame.Fingers;
                    Finger singleFinger = handFinger[0];
                    Screen screen = arg0.LocatedScreens.ClosestScreenHit(singleFinger);

                    if (screen != null && screen.IsValid.Equals(true))
                    { 
                        var tipVelocity = (int)singleFinger.TipVelocity.Magnitude;
                        if (tipVelocity > 1)
                        {   float xScrIntersect = (float)(screen.Intersect(singleFinger, true).x);
                            float yScrIntersect = (float)(1 - screen.Intersect(singleFinger, true).y);
                              
                            if (xScrIntersect.ToString() != "NaN")
                            {
                                if (fingerPoint.Count <= 0)
                                {
                                    fingerPoint.Add(new FingerPoint(xScrIntersect, yScrIntersect, numActiveFinger));
                                }
                                else
                                {
                                    fingerPoint[0].g_X = xScrIntersect;
                                    fingerPoint[0].g_Y = yScrIntersect;
                                    fingerPoint[0].isActive = true;
                                }
                            }
                        }
                    }
                }
                /* agar waktu selalu terupdate */
                //Console.Write("current time: ",currTime,"previous time: ",prevTime);
                prevTime = currTime;
            }
        }
    }
    public class FingerPoint
    {
        public float g_X,g_Y;
        public short numFing;
        public bool isActive;

        public FingerPoint(float x,float y,short num_finger)
        {
            g_X = x;
            g_Y = y;
            isActive = true;
            numFing = num_finger;
        }
    }
}